<template>
  <div class="com-container">
    <div class="title" :style="comStyle">
      <span>{{ '▎ ' + title }}</span>
      <span
        class="iconfont title-icon"
        @click="showChoice = !showChoice"
        :style="comStyle"
        >&#xe6eb;</span
      >
      <div class="select-con" v-if="showChoice">
        <div
          class="select-item"
          v-for="item in selectTypes"
          :key="item.key"
          @click="handleSelect(item.key)"
          :style="marginStyle"
        >
          {{ item.text }}
        </div>
      </div>
    </div>
    <div class="com-chart" ref="trend_ref"></div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // echarts实例对象
      chartInstance: null,
      // 服务器获取所有数据
      allData: [],
      dataType: 'map',
      //   控制可选面板的显示与隐藏
      showChoice: false,
      titleFontSize: 25
    }
  },
  computed: {
    selectTypes() {
      if (!this.allData || !this.allData.type) {
        return []
      } else {
        return this.allData.type.filter(item => {
          return item.key !== this.dataType
        })
      }
    },
    title() {
      if (!this.allData) {
        return ''
      } else {
        return this.allData[this.dataType].title
      }
    },
    comStyle() {
      return {
        fontSize: this.titleFontSize + 'px'
      }
    },
    marginStyle() {
      return {
        marginLeft: this.titleFontSize + 'px'
      }
    }
  },
  created() {
    this.$socket.registerCallBack('trendData', this.getData)
  },
  mounted() {
    // 由于初始化echarts实例对象需要使用到dom元素,因此必须要放到mounted中, 而不是created
    this.initChart()
    // this.getData()
    this.$socket.send({
      action: 'getData',
      socketType: 'trendData',
      chartName: 'trend'
    })
    // 对浏览器窗口大小的变化进行监听
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  destroyed() {
    this.$socket.unRegisterCallBack('trendData')
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.trend_ref, 'chalk')
    },
    async getData() {
      const { data: res } = await this.$http.get('trend')
      this.allData = res
      console.log(this.allData)
      this.updateChart()
    },
    updateChart() {
      // x轴数据
      const timeArrs = this.allData.common.month
      // y轴的数据, 暂时先取出map这个节点的数据
      // map代表地区销量趋势
      // seller代表商家销量趋势
      // commodity代表商品销量趋势
      const valueArrs = this.allData[this.dataType].data
      // 半透明的颜色值
      const colorArr1 = [
        'rgba(11, 168, 44, 0.5)',
        'rgba(44, 110, 255, 0.5)',
        'rgba(22, 242, 217, 0.5)',
        'rgba(254, 33, 30, 0.5)',
        'rgba(250, 105, 0, 0.5)'
      ]
      // 全透明的颜色值
      const colorArr2 = [
        'rgba(11, 168, 44, 0)',
        'rgba(44, 110, 255, 0)',
        'rgba(22, 242, 217, 0)',
        'rgba(254, 33, 30, 0)',
        'rgba(250, 105, 0, 0)'
      ]
      // 图表数据, 一个图表中显示5条折线图
      const seriesArrs = valueArrs.map((item, index) => {
        return {
          type: 'line',
          name: item.name,
          data: item.data,
          // stack值相同, 可以形成堆叠图效果
          stack: this.dataType,
          areaStyle: {
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: colorArr1[index]
              },
              {
                offset: 1,
                color: colorArr2[index]
              }
            ])
          }
        }
      })
      // 准备图例数据, 它需要和series下的每个对象的name属性保持一致
      const legendArr = valueArrs.map(item => {
        return item.name
      })
      const dataOption = {
        grid: {
          top: '35%',
          let: '3%',
          right: '4%',
          bottom: '1%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis'
        },
        xAxis: {
          type: 'category',
          data: timeArrs
        },
        yAxis: {
          type: 'value',
          boundaryGap: false
        },
        legend: {
          data: legendArr,
          top: '15%',
          icon: 'circle',
          left: 20
        },
        series: seriesArrs
      }
      this.chartInstance.setOption(dataOption)
    },
    screenAdapter() {
      this.titleFontSize = (this.$refs.trend_ref.offsetWidth / 100) * 3.6
      const adapterOption = {
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize,
          textStyle: {
            fontSize: this.titleFontSize / 2
          }
        }
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
    handleSelect(key) {
      this.dataType = key
      this.updateChart()
      this.showChoice = false
    }
  }
}
</script>
<style lang="less" scoped>
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 10;
  color: #fff;
  .title-icon {
    margin-left: 10px;
    cursor: pointer;
    background: #222733;
  }
  .select-item {
    cursor: pointer;
  }
}
</style>
